package com.pinganfu.mock.control;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pinganfu.mock.model.MethodEntity;
import com.pinganfu.mock.service.IMethodService;


@Controller
@RequestMapping(value = "/Method", produces = { "application/json;charset=UTF-8"})
public class MethodControl {
	@Resource
	private IMethodService iMethodService;
	
	@RequestMapping(value="/getMethodName",method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody String getMethodName(){

		
		String[] entities = iMethodService.getMethodName();
		
		AbsJsonObject json = new AbsJsonObject(AbsJsonObject.SUCCESS_OK);
   		json.addMessage("mouldNameList", entities);
   		return json.messageToJsonString();
	}
	
	@RequestMapping(value="/getMethodDescription",method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody String getMethodDescription(@RequestParam(required = false) String  methodName){

		MethodEntity entity = new MethodEntity();
		if (null != methodName)
			entity.setMethodName(methodName);
		
		List<MethodEntity> entities = iMethodService.getMethodDescription(entity);
		
		AbsJsonObject json = new AbsJsonObject(AbsJsonObject.SUCCESS_OK);
   		json.addMessage("mouldList", entities);
   		return json.messageToJsonString();
	}
	
	
	@RequestMapping(value="/insertMethod",method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody String insertMethod(
			@RequestParam String methodName,
			@RequestParam String description
			){
		MethodEntity entity = new MethodEntity();
		entity.setMethodName(methodName);
		entity.setDescription(description);
		entity.setCreateTime(new Date());
		
		Integer id = iMethodService.insertMethod(entity);
		
		AbsJsonObject json = new AbsJsonObject(AbsJsonObject.SUCCESS_OK);
   		json.addMessage("insertMethod", id);
   		return json.messageToJsonString();
	}
	
	@RequestMapping(value="/updateMethod",method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody String updateMethod(
			@RequestParam Integer id,
			@RequestParam String methodName,
			@RequestParam String description
			){
		MethodEntity entity = new MethodEntity();
		entity.setId(id);
		entity.setMethodName(methodName);
		entity.setDescription(description);
		
		iMethodService.updateMethod(entity);
		
		
		AbsJsonObject json = new AbsJsonObject(AbsJsonObject.SUCCESS_OK);
   		json.addMessage("updateMethod", entity.getId());
   		return json.messageToJsonString();
	}
	
	@RequestMapping(value="/delMethod",method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody String delMethod(
			@RequestParam String methodName
			){
		MethodEntity entity = new MethodEntity();
		entity.setMethodName(methodName);
		
		iMethodService.delMethod(entity);
		
		AbsJsonObject json = new AbsJsonObject(AbsJsonObject.SUCCESS_OK);
   		json.addMessage("delMethod", entity.getId());
   		return json.messageToJsonString();
	}
}
